package com.jie.commonlib.network.http

import android.os.Handler
import android.os.Looper
import com.jie.commonlib.network.bean.PageInfo
import com.jie.commonlib.network.bean.ResponseBody
import com.jie.commonlib.network.http.parse.DataHandler
import com.jie.commonlib.network.http.utils.GenericsUtils.getInterfaceT
import com.net.okhttphelper.http.DataCallBack
import java.util.*

/**
 * 网络请求回调处理
 * @author jie
 */
@Suppress("UNCHECKED_CAST")
class HttpCallBackHandle<T>(private val mHttpCallBack: HttpCallBack<T>?) : DataCallBack {
    private val mDataHandler: DataHandler = DataHandler()
    private val mHandler = Handler(Looper.getMainLooper())
    override fun success(response: String) {
        if (mHttpCallBack != null) {
            val classList = getInterfaceT(mHttpCallBack)
            if (classList.isNotEmpty()) {
                val firstClass = classList[0]
                when {
                    firstClass == ResponseBody::class.java -> {
                        dealClass(response, classList, false)
                    }
                    firstClass != String::class.java -> {
                        dealClass(response, classList, true)
                    }
                    else -> {
                        postSuccessResult(response as T)
                    }
                }
            } else { // 泛型为空，不做处理，应该会转换为Object
                postSuccessResult(response as T)
            }
        }
    }

    /**
     * 处理泛型
     *
     * @param response 网络返回值
     * @param classArrayList 父类泛型
     * @param isFirstClass 区分第一层级调用和第二层级调用
     */
    @Suppress("UNCHECKED_CAST")
    private fun dealClass(
        response: String,
        classArrayList: ArrayList<Class<*>>,
        isFirstClass: Boolean
    ) {
        val tClass = if (isFirstClass) classArrayList[0] else classArrayList[1]
        val isList = tClass == MutableList::class.java
        val isPageInfo = tClass == PageInfo::class.java
        val responseBody = when {
            isList -> {
                val childClass = if (isFirstClass) classArrayList[1] else classArrayList[2]
                mDataHandler.parseArrayListResponseBody(response, childClass)
            }
            isPageInfo -> {
                val childClass = if (isFirstClass) classArrayList[1] else classArrayList[2]
                mDataHandler.parsePageInfoResponseBody(response, childClass)
            }
            else -> {
                mDataHandler.parseObjectResponseBody(response, tClass)
            }
        }
        if (responseBody.requestIsSuccess()) {
            if (isFirstClass) {
                postSuccessResult(responseBody.data as T)
            } else {
                postSuccessResult(responseBody as T)
            }
        } else {
            postFailResult(responseBody.code, responseBody.msg)
        }
    }

    override fun fail(code: Int, msg: String) {
        postFailResult(code, msg)
    }

    /**
     * 切换到主现场，并返回数据
     */
    private fun postSuccessResult(t: T) {
        mHandler.post { mHttpCallBack?.success(t) }
    }

    /**
     * 切换到主线程，并返回失败原因
     */
    private fun postFailResult(code: Int, msg: String?) {
        mHandler.post { mHttpCallBack?.fail(code, msg) }
    }

}